package com.aihna.mall.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.mall.domain.bo.StoreProductPropertyValueBo;
import com.aihna.mall.domain.vo.StoreProductPropertyValueVo;
import com.aihna.mall.domain.StoreProductPropertyValue;
import com.aihna.mall.mapper.StoreProductPropertyValueMapper;
import com.aihna.mall.service.IStoreProductPropertyValueService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品属性值Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-03-24
 */
@RequiredArgsConstructor
@Service
public class StoreProductPropertyValueServiceImpl implements IStoreProductPropertyValueService {

    private final StoreProductPropertyValueMapper baseMapper;

    /**
     * 查询商品属性值
     */
    @Override
    public StoreProductPropertyValueVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品属性值列表
     */
    @Override
    public TableDataInfo<StoreProductPropertyValueVo> queryPageList(StoreProductPropertyValueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreProductPropertyValue> lqw = buildQueryWrapper(bo);
        Page<StoreProductPropertyValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品属性值列表
     */
    @Override
    public List<StoreProductPropertyValueVo> queryList(StoreProductPropertyValueBo bo) {
        LambdaQueryWrapper<StoreProductPropertyValue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreProductPropertyValue> buildQueryWrapper(StoreProductPropertyValueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreProductPropertyValue> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPropertId() != null, StoreProductPropertyValue::getPropertId, bo.getPropertId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StoreProductPropertyValue::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增商品属性值
     */
    @Override
    public Boolean insertByBo(StoreProductPropertyValueBo bo) {
        StoreProductPropertyValue add = MapstructUtils.convert(bo, StoreProductPropertyValue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品属性值
     */
    @Override
    public Boolean updateByBo(StoreProductPropertyValueBo bo) {
        StoreProductPropertyValue update = MapstructUtils.convert(bo, StoreProductPropertyValue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreProductPropertyValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品属性值
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
